<?php
session_start();
require_once('../kelas/fungsi.php');
require_once('../kelas/amankanParam.php');
require_once('../kelas/record.php');
require_once('../kelas/useradmin.php');
$dtGet= $objParamAman->getMethod();
$dtPost = $objParamAman->postMethod();
$on = isset($dtGet['on'])?$dtGet['on']:'';
$objFungsi->inisialisasiProses($on);
	switch ($on) {

	case 'getForEdit':

	$objUseradmin->get('id,nama,alamat,jabatan,lahir,waktu,username,email,aktif',array('id' =>$dtPost['id'] ) );
	echo json_encode($objUseradmin->execute());
	break;
		case 'login':
			$dtPost['password']=sha1($dtPost['password']);
			$dt  = $objUseradmin->doLogin($dtPost['username'],$dtPost['password']);
			echo json_encode($dt);
			break;
			case 'activateUser':
			unset($dtGet['on']);
			$ak = ($dtGet['act']==1)?0:1;
			unset($dtGet['act']);
			$objUseradmin->update(array('aktif'=>$ak),$dtGet);
			$e = $objUseradmin->execute();
			if($e['success']==false){
				echo $objFungsi->alert($e['error_msg']);
			}
			
				echo $objFungsi->redirect(BASEURLADMIN.'?v=master&p=muser');
				break;
		case 'chpass':
		//var_dump($dtPost);exit;
			$nwPass = sha1($dtPost['newpass']);
			$oldPass =  sha1($dtPost['oldpass']);
			$set=array('password'=>$nwPass);
			$wh = array('password'=>$oldPass,'id'=>$_SESSION['adminlogin']['id']);
			$objUseradmin->num_rows($wh);
			$nR = $objUseradmin->execute();
			$jml=isset($nR['result'])?$nR['result'][0]['total']:0;
			if($jml==0){
				$sk = array('success'=>false,'error_msg'=>'Password Lama Salah');
			}
			else{
				$objUseradmin->update($set,$wh);
				//echo $objUseradmin->strQuery;
				//exit;
				$sk = $objUseradmin->execute();

			};
			if($sk['success']==false){
				echo $objFungsi->alert($sk['error_msg']);
			}
			echo $objFungsi->redirect(BASEURLADMIN.'?v=user&p=chpassadmin');
			break;
		case 'simpan':
			$hiddenID = $dtPost['id'];
			$dtPost['aktif']=isset($dtPost['aktif'])?1:0;
			if(isset($dtPost['lahir'])){
				$exLahir = explode('-',$dtPost['lahir']);
				if(strlen($exLahir[0])==4){
					$dtPost['lahir']=date('Y-m-d',strtotime($exLahir[1].'-'.$exLahir[2].'-'.$exLahir[0]));
				}
				else{
				$dtPost['lahir'] = date('Y-m-d',strtotime($dtPost['lahir']));
			}
			}
			$myP = isset($dtPost['myp'])?true:false;
			unset($dtPost['id']);
			unset($dtPost['myp']);
			if ($hiddenID=='') {
				$cU = $objUseradmin->cekUser($dtPost['username'],$dtPost['email']);
				if($cU['ada']==true){
					$hAkhir = array('success'=>false,'error_msg'=>$cU['error_msg']);	
				}else{
					$dtPost['password']=sha1($dtPost['password']);
					unset($dtPost['repass']);
					$objUseradmin->create($dtPost);
					$hAkhir =  array('success' => false,'error_msg'=>$objUseradmin->strQuery );
				};
				
			}
			else{
				if(isset($dtPost['repass'])){
					unset($dtPost['repass']);					
				}
				if(isset($dtPost['password']) && strlen($dtPost['password'])>0){
					$dtPost['password']=sha1($dtPost['password']);
				}
			 	$objUseradmin->num_rows(null,array(),"(username = '".$dtPost['username']."' OR id='".$hiddenID."')");
				$exTot = $objUseradmin->execute();
				$total = isset($exTot['result'])?$exTot['result'][0]['total']:0;
				if($total>1){
					$hAkhir = array('success'=>false,'error_msg'=>'Username Sudah Digunakan Akun Lain');
				}
				else{
					$objUseradmin->update($dtPost,  array('id' =>$hiddenID ) );
					$hAkhir=$objUseradmin->execute();
					if($myP && $hAkhir['success']==true){
						$objUseradmin->resetSessionBasedId($hiddenID);
					}
					
				}
			}
			echo json_encode($hAkhir);
			break;
			case 'logout':
				unset($_SESSION['adminlogin']);
				echo $objFungsi->redirect(BASEURLADMIN);
				break;
		default:
			echo json_encode( array('success' =>false ,'error_msg'=>'Direct Access Not Allowed' ));
			break;
	}
